package com.temi.iot.test;

import com.temi.iot.common.constant.factory.PageFactory;
import com.temi.iot.common.constant.result.Page;
import com.temi.iot.common.constant.result.Result;
import com.temi.iot.exception.BizExceptionEnum;
import com.temi.iot.exception.ThrowsKit;
import com.temi.iot.jwt.DefaultTokenService;
import com.temi.iot.jwt.Login;
import com.temi.iot.jwt.TokenInterface;
import com.temi.iot.jwt.annotion.Permission;
import com.temi.iot.jwt.enums.PermissionEnums;
import com.temi.iot.jwt.model.TokenValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Map;

/**
 * @create 2019-11-25 20-53
 */
@Slf4j
@Api(description = "测试Swagger")
@RestController
@RequestMapping("/test")
public class testController {

    @ApiOperation("无数据成功返回示例")
    @GetMapping("/noDate")
    public Result noData(){
        return Result.SUCCESS;
    }


    @ApiOperation("有数据成功返回示例")
    @GetMapping("/successData")
    public Result successData() throws Exception {

        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        String token =  TokenInterface.TOKEN.getToken(TokenValue.builder()
                .rid(2)
                .per(strings)
                .build());
        return Result.success(token);
    }
    @Permission(PermissionEnums.DOCTOR)
    @ApiOperation("错误返回示例")
    @GetMapping("/errorData")
    public Result errorData(Integer type){
        if(type == 1){
            return Result.error(BizExceptionEnum.ERRPOR_OVER_TIME);
        }else {
            return Result.error(BizExceptionEnum.NO_PERMITION);
        }

    }

    @ApiOperation("分页数据示例")
    @GetMapping("/page")
    public Page pageList(){
        return PageFactory.page(()->new ArrayList<String>());
    }

    @ApiOperation("分页数据示例")
    @GetMapping("/page1")
    public Page pageList1(){
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        return PageFactory.page(()->strings);
    }


    @ApiOperation("异常处理示例")
    @GetMapping("/exception")
    public Result throwEx(){
        Long id = Login.id();
        System.out.println(id);
        ThrowsKit.isFalse(false,BizExceptionEnum.ACCOUNT_FREEZED);
        return null;
    }

    /**
     * 这是个注释
     * @param userName
     * @param password
     * @param request
     * @param response
     * @return
     */

    @PostMapping(value = "/login")
    public Result login( String userName,String password,HttpServletRequest request, HttpServletResponse response){
        String newtoken = null;

        try {

            newtoken =  TokenInterface.TOKEN.getToken(TokenValue.builder()
                    .rid(1)
                    .per(new ArrayList<String>())
                    .build());

        } catch (AuthenticationException e) {
            e.printStackTrace();
            log.error("User {} login fail, Reason:{}", userName, e.getMessage());
            return Result.error(BizExceptionEnum.LOGIN_ERROR);
        } catch (Exception e) {
            return Result.error(BizExceptionEnum.LOGIN_ERROR);
        }
        return Result.success(newtoken);

    }


}
